﻿using IoTAdapter;
using IoTAdapter.BaseCore;
using IoTAdapter.BaseCore.Enums;
using IoTAdapter.Core.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WCS.Entities
{
    public class Device
    {
        #region Property

        /// <summary>
        /// 设备ID
        /// </summary>
        [SugarColumn(IsPrimaryKey = true)]
        public string Id { get; set; }

        /// <summary>
        /// 设备名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 超时连接时间
        /// </summary>
        public int ConnectTimeOut { get; set; } = 10000; //超时时间默认是10s

        /// <summary>
        /// 设备IP
        /// </summary>
        public string IP { get; set; }

        /// <summary>
        /// 设备端口号
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 设备站号
        /// </summary>
        public byte Station { get; set; } = 1;

        /// <summary>
        /// 设备是否启用
        /// </summary>
        public bool Enable { get; set; } = true;

        /// <summary>
        /// 是否从零开始
        /// </summary>
        public bool IsStartWithOne { get; set; }

        /// <summary>
        /// 设备是否在重连
        /// </summary>
        public bool IsReconecting { get; set; }

        /// <summary>
        /// 当不同的设备有不同的情况监控的时候，根据这个写不同的类。
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string MonitorType { get; set; }

        /// <summary>
        /// 设备型号
        /// </summary>
        public EthernetDeviceVersion Type { get; set; }


        private IReadWrite operation;
        /// <summary>
        /// 设备操作读写打开接口
        /// </summary>
        public IReadWrite Operation
        {
            get
            {
                //if (operation == null)
                    operation ??= IoTClientFactory.CreateClientEthernetDevice(Type, IP, Port, Station, 30000, IsStartWithOne, DataFormat.CDAB);

                return operation;
            }
        }

        /// <summary>
        /// 设备所有的传送对接口
        /// </summary>
        [Navigate(NavigateType.OneToMany, nameof(TransferPort.DeviceId))]
        public List<TransferPort> TransferPorts { get; set; }

        public bool IsConnected => Operation?.IsConnected ?? false;
        #endregion


        #region Method




        /// <summary>
        /// 连接设备
        /// </summary>
        public bool Connect()
        {

            if (Enable && !IsConnected)
            {
                return Operation?.Connect()?.IsSucceed ?? false;

            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <param name="isDispose"></param>
        public void Close(bool isDispose)
        {
            Operation?.Close();
        }


        #endregion

        #region 重写Equal
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (ReferenceEquals(obj, this))
            {
                return true;
            }
            return false;
            //return obj is Robot && Id == (obj as Robot).Id;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
        #endregion
    }
}
